package org.neframework.webtest.action.xyk.pos;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.neframework.webtest.base.BaseController;
import org.neframework.webtest.model.t_s_bank;
import org.neframework.webtest.model.t_s_customer;
import org.neframework.webtest.model.t_s_customer_card;
import org.neframework.webtest.model.t_s_customer_card_pos;
import org.neframework.webtest.service.CustomerService;

import com.tmsps.neframework.core.utils.ChkTools;
import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
@RequestMapping("/cp/xyk/pos")
public class PosController extends BaseController {

	private static final CustomerService custService = new CustomerService();

	@RequestMapping("/list")
	public String list(String customer_card_id) {
		t_s_customer_card card = (t_s_customer_card) oc.findObjById(
				customer_card_id, t_s_customer_card.class);
		t_s_customer customer = (t_s_customer) oc.findObjById(
				card.getCustomer_id(), t_s_customer.class);
		t_s_bank bank = (t_s_bank) oc.findObjById(card.getBank_id(),
				t_s_bank.class);

		req.setAttribute("card", card);
		req.setAttribute("customer", customer);
		req.setAttribute("bank", bank);
		return "/jsp/xyk/pos/list.jsp";
	}

	@RequestMapping("/list_data")
	@ResponseBody
	public void list_data(String customer_card_id) {

		List<Map<String, Object>> list = custService.selectCustomerCardPosList(
				customer_card_id, sort_params, page);

		result.put("list", list);
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(t_s_customer_card_pos pos, String rate_money_comp) {
		t_s_customer_card card = (t_s_customer_card) oc.findObjById(
				pos.getCustomer_card_id(), t_s_customer_card.class);
		t_s_customer customer = (t_s_customer) oc.findObjById(
				card.getCustomer_id(), t_s_customer.class);

		pos.setCard_no(card.getCard_no());
		pos.setCustomer_id(customer.getKid());

		BigDecimal remain = card.getMoney_remain();
		if ("消费".equals(pos.getType())) {
			remain = remain.subtract(pos.getMoney());

			// pos.setRate_money(pos.getMoney().multiply(pos.getRate()));
			pos.setRate_money(ChkTools.getBigDecimal(rate_money_comp));

		} else if ("还款".equals(pos.getType())) {
			remain = remain.add(pos.getMoney());
		} else {
			throw new RuntimeException("刷卡类型有问题!");
		}

		card.setMoney_remain(remain);
		pos.setMoney_remain(remain);

		oc.updateObj(card);
		oc.saveObj(pos);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String kid) {
		t_s_customer_card_pos pos = (t_s_customer_card_pos) oc.findObjById(kid,
				t_s_customer_card_pos.class);

		Map<String, Object> map1 = JsonTools.objToMap("pos", pos);
		map1.put("pos_money_remain", pos.getMoney_remain());
		map1.put("pos_money", pos.getMoney());
		map1.put("pos_rate", pos.getRate());

		return JsonTools.toJson(map1);
	}

	@RequestMapping("/edit")
	@ResponseBody
	public void edit(t_s_customer_card_pos pos) {

		t_s_customer_card_pos posDb = (t_s_customer_card_pos) oc.findObjById(
				pos.getKid(), t_s_customer_card_pos.class);

		t_s_customer_card card = (t_s_customer_card) oc.findObjById(
				posDb.getCustomer_card_id(), t_s_customer_card.class);

		BigDecimal remain = card.getMoney_remain();
		if ("消费".equals(pos.getType())) {
			remain = remain.subtract(pos.getMoney().subtract(posDb.getMoney()));
		} else if ("还款".equals(pos.getType())) {
			remain = remain.add(pos.getMoney().subtract(posDb.getMoney()));
		} else {
		}
		card.setMoney_remain(remain);

		posDb.setAddress(pos.getAddress());
		posDb.setNote(pos.getNote());
		posDb.setRate(pos.getRate());
		// posDb.setType(pos.getType());
		posDb.setMoney_remain(pos.getMoney_remain());
		posDb.setMoney(pos.getMoney());

		oc.updateObj(posDb);
		oc.updateObj(card);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(String kid) {

		t_s_customer_card_pos pos = (t_s_customer_card_pos) oc.findObjById(kid,
				t_s_customer_card_pos.class);
		t_s_customer_card card = (t_s_customer_card) oc.findObjById(
				pos.getCustomer_card_id(), t_s_customer_card.class);

		BigDecimal remain = card.getMoney_remain();
		if ("消费".equals(pos.getType())) {
			remain = remain.add(pos.getMoney());
		} else if ("还款".equals(pos.getType())) {
			remain = remain.subtract(pos.getMoney());
		} else {
		}

		card.setMoney_remain(remain);

		oc.updateObj(card);
		oc.delObj(pos);

		this.setTipMsg(true, "删除成功!", Tip.Type.success);
	}

}
